/**

 * @author admin

 * 2014年7月16日

 */

package com.tmall.testgen.plugin.wizards.runinspring;

import java.lang.reflect.Method;

import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TreeEditor;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;

import com.tmall.testgen.plugin.views.vo.SpringTree;
import com.tmall.testgen.plugin.views.vo.TreeParent;
import com.tmall.testgen.plugin.views.vo.ViewContentProvider;
import com.tmall.testgen.plugin.views.vo.ViewLabelProvider;
import com.tmall.testgen.plugin.wizards.runinspring.listener.CodeInputListener;
import com.tmall.testgen.plugin.wizards.runinspring.listener.CopyInputListener;
import com.tmall.testgen.plugin.wizards.runinspring.listener.DeleteTestTreeListener;
import com.tmall.testgen.plugin.wizards.runinspring.listener.TestDescriptionInputListener;
import com.tmall.testgen.plugin.wizards.runinspring.listener.TreeSelectListener;

public class TreeCreator {
	public static void initTree(RunInSpringWizadPage page) {
		initTree(page, false);
	}

	public static void initTree(RunInSpringWizadPage page, boolean isClean) {

		/**
		 * 初始化结果区域
		 */
		SpringTree resultRoot = new SpringTree();
		resultRoot.setEditable(false);
		page.setResultRoot(resultRoot);
		/**
		 * 初始化参数区域
		 */
		SpringTree parasRoot = new SpringTree();
		page.setParasRoot(parasRoot);
		/**
		 * 初始化生成代码区域
		 */
		if (!isClean) {
			SpringTree genCodeRoot = new SpringTree();
			genCodeRoot.setEditable(false);
			page.setGenCodeRoot(genCodeRoot);
			
			
			TreeParent interceptRoot = new TreeParent("测试库");
	        interceptRoot.setLeafNode(false);
	        genCodeRoot.addChild(interceptRoot);
	        page.setGenCodeInterceptRoot(interceptRoot);
	        
	        TreeParent mockRoot = new TreeParent("mock库");
	        mockRoot.setLeafNode(false);
            genCodeRoot.addChild(mockRoot);
			page.setGenCodeMockRoot(mockRoot);
		}

		Method testMethod = page.getTestMethod();
		String methodName = page.getMethodName();

		Class[] clazz = testMethod.getParameterTypes();
		String content = methodName.substring(methodName.indexOf("(") + 1,
				methodName.indexOf(")"));
		String[] params = content.split(",");

		for (int i = 0; i < params.length; i++) {
			String param = params[i];
			param = param.trim();
			if (param.length() == 0)
				continue;

			while (param.indexOf("  ") > 0) {
				param.replaceAll("  ", " ");
			}
			String[] p = param.split(" ");
			SpringTree st = new SpringTree();
			st.setClazz(clazz[i]);
			st.setFieldName(p[1]);
			// st.setLeafNode(RunInSpringUtil.isBasicType(st.getClazz()));
			st.initName();
			st.setLeafNode(st.getValue() == null);
			parasRoot.addChild(st);
		}
	}

	public static void createResultTree(Composite com,
			SpringTree resultTreeRoot, RunInSpringWizadPage page) {
		final TreeViewer resultTreeViewer = new TreeViewer(com, SWT.SINGLE
				| SWT.H_SCROLL | SWT.V_SCROLL);
		page.setResultTreeViewer(resultTreeViewer);
		GridData comGd = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1);

		final Tree tree = resultTreeViewer.getTree();
		tree.setLayoutData(comGd);
		final Menu menu = createTreePopMenu(tree.getShell(), resultTreeViewer,
				false);
		tree.setMenu(menu);
		resultTreeViewer.setContentProvider(new ViewContentProvider());
		resultTreeViewer.setLabelProvider(new ViewLabelProvider());
		resultTreeViewer.setInput(resultTreeRoot);

		final TreeEditor treeEditor = new TreeEditor(tree);
		treeEditor.horizontalAlignment = SWT.LEFT;
		treeEditor.grabHorizontal = true;
		treeEditor.minimumWidth = 30;
		tree.addSelectionListener(new TreeSelectListener(menu, treeEditor,
				resultTreeViewer, tree,page));
	}

	public static void createGenCodeTree(Composite com,
			TreeParent genCodeTreeRoot, RunInSpringWizadPage page) {
		final TreeViewer genCodeTreeViewer = new TreeViewer(com, SWT.SINGLE
				| SWT.H_SCROLL | SWT.V_SCROLL);
		page.setGenCodeTreeViewer(genCodeTreeViewer);
		GridData comGd = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1);

		final Tree tree = genCodeTreeViewer.getTree();
		tree.setLayoutData(comGd);
		final Menu menu = createTreePopMenu(tree.getShell(), genCodeTreeViewer,
				false);
		
		MenuItem desItem = new MenuItem(menu, SWT.PUSH);
		desItem.setText("单元测试描述");
		desItem.addSelectionListener(new TestDescriptionInputListener( genCodeTreeViewer,tree.getShell()));
        
        
        
		MenuItem item = new MenuItem(menu, SWT.PUSH);
		item.setText("删除");
		item.addSelectionListener(new DeleteTestTreeListener(genCodeTreeViewer));
		
		
		
        
        
        
		tree.setMenu(menu);
		genCodeTreeViewer.setContentProvider(new ViewContentProvider());
		genCodeTreeViewer.setLabelProvider(new ViewLabelProvider());
		genCodeTreeViewer.setInput(genCodeTreeRoot);

		final TreeEditor treeEditor = new TreeEditor(tree);
		treeEditor.horizontalAlignment = SWT.LEFT;
		treeEditor.grabHorizontal = true;
		treeEditor.minimumWidth = 30;
		tree.addSelectionListener(new TreeSelectListener(menu, treeEditor,
				genCodeTreeViewer, tree,page));
	}

	public static void createParasTree(Composite com, SpringTree parasRoot,
			RunInSpringWizadPage page) {
		final TreeViewer parasTreeViewer = new TreeViewer(com, SWT.SINGLE
				| SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
		page.setParasTreeViewer(parasTreeViewer);
		GridData comGd = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1);
		final Tree tree = parasTreeViewer.getTree();
		tree.setLayoutData(comGd);
		final Menu menu = createTreePopMenu(tree.getShell(), parasTreeViewer,
				true);
		tree.setMenu(menu);
		parasTreeViewer.setContentProvider(new ViewContentProvider());
		parasTreeViewer.setLabelProvider(new ViewLabelProvider());
		parasTreeViewer.setInput(parasRoot);

		final TreeEditor treeEditor = new TreeEditor(tree);
		treeEditor.horizontalAlignment = SWT.LEFT;
		treeEditor.grabHorizontal = true;
		treeEditor.minimumWidth = 30;
		tree.addSelectionListener(new TreeSelectListener(menu, treeEditor,
				parasTreeViewer, tree,page));
	}

	private static Menu createTreePopMenu(final Shell shell,
			final TreeViewer treeViewer, boolean isParasTree) {
		Menu menu = new Menu(shell, SWT.POP_UP);
		if (isParasTree) {
			// 只有属性树 才添加 手工输入菜单
			MenuItem item = new MenuItem(menu, SWT.PUSH);
			item.setText("手工输入");
			item.addSelectionListener(new CodeInputListener(treeViewer, shell));
		}
		MenuItem item = new MenuItem(menu, SWT.PUSH);
		item.setText("copy");
		item.addSelectionListener(new CopyInputListener(treeViewer, shell));
		return menu;
	}
}
